﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cmsType。
    /// </summary>
    public class cmsType
    {
        public cmsType()
        {
            DbHelperSQL.connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int TypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cmsType");
            strSql.Append(" where TypeId=" + TypeId + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cmsType model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.TypeOrder != null)
            {
                strSql1.Append("TypeOrder,");
                strSql2.Append("" + model.TypeOrder + ",");
            }
            if (model.TypeName != null)
            {
                strSql1.Append("TypeName,");
                strSql2.Append("'" + model.TypeName + "',");
            }
            if (model.TypeKey != null)
            {
                strSql1.Append("TypeKey,");
                strSql2.Append("'" + model.TypeKey + "',");
            }
            if (model.Purview != null)
            {
                strSql1.Append("Purview,");
                strSql2.Append("'" + model.Purview + "',");
            }
            if (model.EditPurview != null)
            {
                strSql1.Append("EditPurview,");
                strSql2.Append("'" + model.EditPurview + "',");
            }
            if (model.ImgFile != null)
            {
                strSql1.Append("ImgFile,");
                strSql2.Append("'" + model.ImgFile + "',");
            }
            if (model.Del != null)
            {
                strSql1.Append("Del,");
                strSql2.Append("" + (model.Del ? 1 : 0) + ",");
            }
          
            strSql.Append("insert into cmsType(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cmsType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cmsType set ");
            if (model.TypeOrder != null)
            {
                strSql.Append("TypeOrder=" + model.TypeOrder + ",");
            }
            if (model.TypeName != null)
            {
                strSql.Append("TypeName='" + model.TypeName + "',");
            }
            if (model.TypeKey != null)
            {
                strSql.Append("TypeKey='" + model.TypeKey + "',");
            }
            if (model.Purview != null)
            {
                strSql.Append("Purview='" + model.Purview + "',");
            }
            if (model.EditPurview != null)
            {
                strSql.Append("EditPurview='" + model.EditPurview + "',");
            }
            if (model.ImgFile != null)
            {
                strSql.Append("ImgFile='" + model.ImgFile + "',");
            }
            if (model.Del != null)
            {
                strSql.Append("Del=" + (model.Del ? 1 : 0) + ",");
            }
          
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where TypeId=" + model.TypeId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int TypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cmsType ");
            strSql.Append(" where TypeId=" + TypeId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cmsType GetModel(int TypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" TypeId,TypeOrder,TypeName,TypeKey,Purview,EditPurview,ImgFile,Del ");
            strSql.Append(" from cmsType ");
            strSql.Append(" where TypeId=" + TypeId + " ");
            Usas.Model.Cms.cmsType model = new Usas.Model.Cms.cmsType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TypeId"].ToString() != "")
                {
                    model.TypeId = int.Parse(ds.Tables[0].Rows[0]["TypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeOrder"].ToString() != "")
                {
                    model.TypeOrder = int.Parse(ds.Tables[0].Rows[0]["TypeOrder"].ToString());
                }
                model.TypeName = ds.Tables[0].Rows[0]["TypeName"].ToString();
                model.TypeKey = ds.Tables[0].Rows[0]["TypeKey"].ToString();
                model.Purview = ds.Tables[0].Rows[0]["Purview"].ToString();
                model.EditPurview = ds.Tables[0].Rows[0]["EditPurview"].ToString();
                model.ImgFile = ds.Tables[0].Rows[0]["ImgFile"].ToString();
                if (ds.Tables[0].Rows[0]["Del"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Del"].ToString() == "1") || (ds.Tables[0].Rows[0]["Del"].ToString().ToLower() == "true"))
                    {
                        model.Del = true;
                    }
                    else
                    {
                        model.Del = false;
                    }
                }
                
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TypeId,TypeOrder,TypeName,TypeKey,Purview,EditPurview,ImgFile,Del ");
            strSql.Append(" FROM cmsType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" TypeId,TypeOrder,TypeName,TypeKey,Purview,EditPurview,ImgFile,Del ");
            strSql.Append(" FROM cmsType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}

